package com.sinodata.bsm.cicp.collector.weblogic8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:04:36   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ExecuteQueueCollector extends Collector {

    private static final Logger logger = Logger.getLogger(ExecuteQueueCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        String port = this.getParamMap().get("Port").trim();//端口
        String username = this.getParamMap().get("Username").trim();//用户名
        String password = this.getParamMap().get("Password").trim();//密码
        String serverName = this.getParamMap().get("ServiceName").trim();//服务名称
        String webName = this.getParamMap().get("Name").trim();//名称
        String xmlString = XmlGetter.getJmxXml("WlgExecuteQueueInfo", ip, port, username, password);
        // parse xml
        Document document;
        try {
            document = DocumentHelper.parseText(xmlString);
            Element rootElement = document.getRootElement();
            Iterator<?> iterator = rootElement.elementIterator("ExecuteQueueInfo");
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element != null) {
                    String svrName = element.element("ServerName").getTextTrim();
                    String nameString = element.element("Name").getTextTrim();
                    if (!svrName.equals(serverName)) {
                        continue;
                    } else if (nameString.equals(webName)) {
                        ResPropertyValue propertyValueX = new ResPropertyValue();
                        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                        String pendingRequestCurrentCount = element.element("PendingRequestCurrentCount").getTextTrim();
                        String executeThreadTotalCount = element.element("ExecuteThreadTotalCount").getTextTrim();
                        String servicedRequestTotalCount = element.element("ServicedRequestTotalCount").getTextTrim();
                        String executeThreadCurrentIdleCount = element.element("ExecuteThreadCurrentIdleCount").getTextTrim();

                        propValues[0] = ResPropertyValue.createPropValue(pendingRequestCurrentCount, propMetas[0]);
                        propValues[1] = ResPropertyValue.createPropValue(executeThreadTotalCount, propMetas[1]);
                        propValues[2] = ResPropertyValue.createPropValue(servicedRequestTotalCount, propMetas[2]);
                        propValues[3] = ResPropertyValue.createPropValue(executeThreadCurrentIdleCount, propMetas[3]);
                        /*
                         * values.add(new String[] { pendingRequestCurrentCount,
                         * executeThreadTotalCount, servicedRequestTotalCount,
                         * executeThreadCurrentIdleCount });
                         */
                        propertyValueX.setValues(propValues);
                        values.add(propertyValueX);
                        return values;
                    }
                }
            }

        } catch (DocumentException e) {
            logger.error("failed to collect by ExecuteQueueCollector", e);
            throw new CollectorException(e);
        }
        return null;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "用户名", "密码", "名称", "服务名称" };
        return new String[] { "Port", "Username", "Password", "Name", "ServiceName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "等待请求数", "执行线程总数", "服务请求总数", "空闲线程数" };
        return new String[] { "WaitRequestsNum", "TotalExecuteThreadsNum", "TotalServiceRequestsNum", "FreeThreadsNum" };
    }

}